﻿using NewsPortal.Modularity.Core;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Web;

namespace NewsPortal.Web.Infrastructure.Modularity
{
    public class DirectoryModuleTypeProvider: IModuleTypeProvider
    {
        private string _path;
        public DirectoryModuleTypeProvider(string path)
        {
            this._path = path;
        }

        public IEnumerable<Type> GetComptabileTypes()
        {
            List<Type> result = new List<Type>();

            Directory.EnumerateFiles(HttpContext.Current.Server.MapPath(this._path))
                .ForEach(path => result.AddRange(Assembly.LoadFrom(path).GetTypes().Where(t => t.IsModuleType(new[] {
                    typeof(ModuleControllerBase<>), typeof(IModule)
                }))));

            return result;
        }
    }
}